package com.atshiyou.webDatabase;

import java.sql.*;

public class ConnectMysql {
    public  static boolean insert(String username,String password) throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/express", "root", "123456");
        PreparedStatement statement = conn.prepareStatement("insert into users2(username,passwd) values(?,?)");
        statement.setString(1,username);
        statement.setString(2,password);
        int i = statement.executeUpdate();
        conn.close();
        statement.close();
        if(i==1){
            return true;
        }else{
            return false;
        }


    }

    public static boolean updatePoint(String username,int point) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/express", "root", "123456");
        PreparedStatement statement = conn.prepareStatement("update  users2 set point = ? where username = ?");
        statement.setInt(1,point);
        statement.setString(2,username);
        int i = statement.executeUpdate();
        conn.close();
        statement.close();
        //数据库中的username重名的话，会有bug
        if(i==1){
            return true;
        }else{
            return false;
        }
    }

    public static boolean checkUser(String username,String password) throws Exception {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/express", "root", "123456");
            statement = conn.prepareStatement("select * from users2 where username=? and passwd = ?");
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();


            if (resultSet.next()) {
                return true;
            }
            //resultSet.close();
            return false;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            conn.close();
            statement.close();
        }
        return false;
    }

    public static  int findPoint(String username) throws SQLException {
        Connection conn = null;
        PreparedStatement statement = null;
        int point = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/express", "root", "123456");
            statement = conn.prepareStatement("select point from users2 where username=?");
            statement.setString(1, username);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                point = resultSet.getInt("point");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            conn.close();
            statement.close();
        }
        return point;
    }



}
